package com.yulongtian.algorithms.binarysearch01;

/**
 * https://leetcode.cn/problems/sum-of-square-numbers/?envType=study-plan&id=binary-search-beginner&plan=binary-search&plan_progress=blax59t
 * 可
 *
 * @author yulongTian
 * @create 2023-02-09 11:43
 */
public class Test11 {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(judgeSquareSum(Integer.MAX_VALUE));
    }

    public static boolean judgeSquareSum(int c) {
        if (c <= 2) {
            return true;
        }
        int last = (int) Math.sqrt(c);
        for (int a = 0; a <= last; a++) {
            int left = a;
            int right = last;
            int target = c - a * a;
            while (left <= right) {
                int b = left + (right - left) / 2;
                if (b * b == target) {
                    return true;
                }
                if (b * b > target) {
                    right = b - 1;
                }
                if (b * b < target) {
                    left = b + 1;
                }
            }
        }
        return false;
    }

}
